/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asistencia.DAO;

import com.asistencia.DAO.utils.Connector;
import com.asistencia.DAO.utils.InterfaceDAO;
import com.asistencia.model.Perfil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



/**
 *
 * @author rfarias
 * Clase de Métodos de datos para perfiles
 */
public class PerfilDAO implements InterfaceDAO  {

    public List findAll() {
        Perfil per = null;
        ArrayList<Perfil> listaper = new ArrayList<Perfil>();
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from Perfil");
            while(rs.next())
            {
                per = new Perfil(
                        rs.getString("descripcion"),
                        rs.getString("alcance")
                        );
                per.setId_perfil(rs.getInt("id_perfil"));
                listaper.add(per);
            }
        } catch (Exception ex){
            System.out.println("ERROR: PerfilDAO - Method: findAll - Exception: " + ex.getMessage());
        } finally {
        return listaper;
        }
    }



    public Object searchById(int id) {
        Perfil per = null;
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from Perfil where id_Perfil = " + id);
            if(rs.next()) {
                per = new Perfil(rs.getString("descripcion"), rs.getString("alcance"));
            }
                per.setId_perfil(id);
        } catch (Exception ex){
            System.out.println("ERROR: PerfilDAO - Method: SearchById - Exception: " + ex.getMessage());
        } finally {

            return per;
        }
    }

    public Object searchById(String id) {
        return null;
    }

    public Object update(Object obj) {
        Perfil per = (Perfil)obj;
        Statement st = Connector.getStatement();
        StringBuilder sb = new StringBuilder();
        sb.append("update Perfil set ");
        sb.append ("descripcion='");sb.append(per.getDescripcion());sb.append("', ");
        sb.append ("alcance='");sb.append(per.getAlcance());sb.append("' where id_perfil=");sb.append(per.getId_perfil());
        try {
            int res = st.executeUpdate(sb.toString());
            if (res==1)
            {
                return per;
            }
            else
            {
                return null;
            }
        } catch (SQLException ex) {
           System.out.println("ERROR: PerfilDAO - Method: update - Exception: " + ex.getMessage());
           return null;
        }

    }

    public Object insert(Object obj) {
        Perfil per = (Perfil)obj;
        try{
            StringBuilder sb = new StringBuilder();
            sb.append("insert into Perfil (descripcion, alcance) values(");
            sb.append("'");sb.append(per.getDescripcion());sb.append("',");
            sb.append("'");sb.append(per.getAlcance());sb.append("' )");

            Statement st = Connector.getStatement();
            int res = st.executeUpdate(sb.toString(), Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = st.getGeneratedKeys();
            if(rs.next()) {
                per.setId_perfil(rs.getInt(1));
            }

        } catch (Exception ex){
            System.out.println("ERROR: PerfilDAO - Method: insert - Exception: " + ex.getMessage());
        } finally {
            return String.valueOf(per.getId_perfil()) != null ? searchById(per.getId_perfil().intValue()) : null;
        }

    }

 public boolean delete(Object obj) {
        Perfil per = (Perfil) obj;
        Statement st = Connector.getStatement();
        try {
            int res = st.executeUpdate("delete from Perfil where id_perfil="+per.getId_perfil()+" ");
             if (res>0)
                {
                   return true;
                }
             else
                {
                    return false;
                }
        } catch (SQLException ex) {
            System.out.println("ERROR: PerfilDAO - Method: delete - Exception: " + ex.getMessage());
            return false;
        }
    }


  public Object searchByDesc(String desc) {
        Perfil per = null;
        try{
            ResultSet rs = Connector.getStatement().executeQuery("select * from Perfil where descripcion= '" + desc+"'");
            if(rs.next()) {
                per = new Perfil(rs.getString("descripcion"), rs.getString("Alcance"));
            }
                per.setId_perfil(rs.getInt("id_Perfil"));
        } catch (Exception ex){
            System.out.println("ERROR: PerfilDAO - Method: searchByDesc - Exception: " + ex.getMessage());
        } finally {

            return per;
        }
    }



}
